﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GoodTeacherInput.aspx.cs" Inherits="Credit_Teacher_ScoreInput_GoodTeacherInput" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <link href="../../Common/css/main.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../../Common/js/jquery.js"></script>
    <script type="text/javascript" src="../../Common/js/common.js"></script>
    <script type="text/javascript" src="../../Common/js/jsUtil.js"></script>
    <script type="text/javascript" src="../../../../Common/js/ScoreInputUtil.js"></script>
    <script type="text/javascript" src="../../../Common/js/TableForExport.js"></script>

    <script type="text/javascript">
    var historyId = 0;
    var teacherID = 0;
    // 加次数
    function add(obj) {
        var txt = obj.parentNode.parentNode.firstChild.firstChild;
        var val = parseFloat(txt.value);
        if (isNaN(val)) val = 0;
        txt.value = ++val;
    }
    
    // 减次数
    function minus(obj) {
        var txt = obj.parentNode.parentNode.firstChild.firstChild;
        var val = parseFloat(txt.value);
        if (isNaN(val)) val = 0;
        txt.value = val == 0 ? 0 : --val;
    }
    // 鼠标移动到计次单元格时弹出点击按钮
	function mover(obj) {
		obj.firstChild.firstChild.firstChild.lastChild.style.display = "inline";
	}
    
    // 鼠标移出计次单元格时隐藏点击按钮
	function mout(obj) {
		obj.firstChild.firstChild.firstChild.lastChild.style.display = "none";
	}
    
    // 保存本次结果
    function saveList() {
        var list = [];
        var trList = $("#tblHead tr");
        var stuJsonList = "";
        var jsonList = "";
        for (var i=0; i<trList.length; i++) {
            if ($(trList[i]).find("table").length == 0) continue;
            var inputList = $(trList[i]).find("input");
            var spanRemark = $(trList[i]).find("span")[0];
            var inputState = false;
            var stuList = [];
            var teacherID;
            //debugger;
            for (var j=0; j<inputList.length; j++) {
                var str = inputList[j].id;
                str = str.replace("txt_", "");
                var arr = str.split("_");
                var moralityItemID = parseInt(arr[0]);
                teacherID = parseInt(arr[1]);
                
                var val = inputList[j].value;
                if (val != "" && isNaN(val)) {
                    showNoticMsg(str, "请填写数字");
                    return false;
                }
                if (val == "") continue;
                inputState = true;
              
                var param = parseFloat(val);
                var score = inputList[j].getAttribute("Score") == null ? -1 : parseFloat(inputList[j].getAttribute("Score"));
                var operator = inputList[j].getAttribute("Math") == "" ? 0 : parseFloat(inputList[j].getAttribute("Math"));
                
                var type = parseInt(inputList[j].getAttribute("MoralityType"));
                if (type == 2 && param < 0) {
                    stuList = [];
                    //showNoticMsg(inputList[j].id, "计次题，请填写大于或等于0的次数", 0, -40);
                    showMassgeBox("提示","计次题请填写大于或等于0的次数");
                    return;
                }
                else if(type == 1 && score < param) {
                    //showNoticMsg(inputList[j].id, "计分题，请填写小于最大分数的的数字", 0, -40);
                    showMassgeBox("提示","计分题请填写小于最大分数的的数字");
                    return;
                }
                if (isNaN(operator)) operator = "0";
                var item = "{\"MoralityItemID\": "+moralityItemID+", \"TeacherID\": "+teacherID+", \"Param\": "+param+", \"Score\": "+score+", \"Type\": "+type+", \"Operator\": "+operator+"}";
                stuList.push(item);
            } 
            /* 填写了该学生的资料 */
            if (inputState) {
                stuJsonList = stuList.join(",");
                stuJsonList = "{\"TeacherID\": "+teacherID+",\"TeacherItems\":[" + stuJsonList + "]";
                if (spanRemark != undefined && spanRemark.title != "") stuJsonList += ", \"Remark\": \"" + spanRemark.title + "\"";
                stuJsonList += "}";
                list.push(stuJsonList);
            }
            /* 只填了备注 */
            else {
                if (spanRemark != undefined && spanRemark.title != "") {
                    studentID = parseInt(spanRemark.id.replace("span_",""));
                    stuJsonList = "{\"TeacherID\": "+teacherID+",\"Remark\": \"" + spanRemark.title + "\"}";
                    list.push(stuJsonList);
                }
            }
        }
        if (stuJsonList.length > 0) {
            jsonList = list.join(",");
            jsonList = "[" + jsonList + "]";
        }
        var schoolYear = request("SchoolYear");
        if (schoolYear == "") schoolYear = -1;
        else schoolYear = parseInt(schoolYear);
        
        var term = request("Term");
        if (term == "") term = -1;
        else term = parseInt(term);
        
        var year = request("Year");
        if (year == "") year = -1;
        else year = parseInt(year);
        
        var month = request("Month");
        if (month == "") month = -1;
        else month = parseInt(month);

        PageMethods.SaveScoreInput(historyId,jsonList, schoolYear, term, year, month,
            function(succ){ if (succ) showMassgeBox("保存成功","保存成功"); else showMassgeBox("保存失败","保存失败, 请检查数据是否录入或者录入的数据是否符合规则."); }, 
            function(err){return;}
        );
    }
    
    function displayRemark(obj) {
        var teacherName = obj.getAttribute("TeacherName");
        ShowPopForm("divInputRemark", "spanInputRemarkTitle", teacherName + "的备注");
        document.getElementById("hidTeacherID").value = obj.getAttribute("TeacherID");
        document.getElementById("txtRemark").value = obj.childNodes[1].title;
    }
    
    function hideRemark() {
        ClosePopForm("divInputRemark");
        document.getElementById("txtRemark").value = "";
    }
    
    function saveRemark() {
        var txtRemark = document.getElementById("txtRemark");
        if (txtRemark.value == "") {
            showNoticMsg("txtRemark", "请填写备注");
            return;
        }
        var title = txtRemark.value;
        var html = "";
        if (txtRemark.value.length > 20) {
            html = txtRemark.value.substr(0, 20) + "...";
        }
        else {
            html = txtRemark.value;
        }
        var id = "span_" + document.getElementById("hidTeacherID").value;
        var spanRemark = document.getElementById(id);
        spanRemark.innerHTML = html;
        spanRemark.title = title;
        hideRemark();
    }
    
    window.onload = function() {
        var btnReset = document.getElementById("btnReset");
        var btnSave = document.getElementById("btnSave");
        var btnSum = document.getElementById("btnSum");
        var btnExport = document.getElementById("btnExport");
        if (request("Type") == "Edit") {
            btnReset.style.display = "inline";
            btnSave.style.display = "inline";
            btnSum.style.display = "inline";
            btnExport.style.display = "none";
        }
        else {
            btnReset.style.display = "none";
            btnSave.style.display = "none";
            btnSum.style.display = "none";
            btnExport.style.display = "inline";
        }
    }
    
    function reflesh() {
        window.location.href = window.document.location.href;
    }
    function CheckCh(avalue)
    {
        for   (n=0;   n<avalue.length;   n++) 
        {  
            if   (avalue[n]>128)//   中文字符
            {
                return false;
            }
        }
        return true;
    }
    function checkIsfloat(objvalue)
    {
        var value = objvalue;
        if( value.search(/^(-|\+)?\d+(\.\d+)?$/)==-1) {   
            if(value.length >0 && value.charAt(value.length-1)==".") {
                value = value.substr(0,value.length-1);
                if( value.search(/^(-|\+)?\d+(\.\d+)?$/)==-1)
                    return false;
                else {
                    if(CheckCh(value))
                        return true;
                    else
                        return false;
                }
            }
            else {
                if(value.length == 0 ||  ( /\D/.test(value)))
                    return false;
                else
                    return true;
            }
        }
        else
            return true; 

        return true;
    }
    //焦点移开后判断更新
    function optionSettingScore(obj, type) {
        var value = obj.value;
        var flag = false;
        if (type == 1) {
            // 计分题
            flag = checkIsfloat(value)
        }
        else {
            // 计次题
            flag = /^[1-9]\d*|0$/.test(value)
        }
        if(value.length == 0 ||  !flag) {   
            obj.setAttribute("value",obj.getAttribute("oldvalue")); 
        }
        else {
            obj.setAttribute("oldvalue",value); 
            settingChange = true;
        }
    }
    function getExcel() {
        var cells = GetTableInfo(document.getElementById("tblHead"));
        if(cells!=null) {            
            var causeID = request("CauseID");
            if (causeID == "") causeID = -1;
            else causeID = parseInt(causeID);
            
            PageMethods.GetExcel(cells,causeID,function(result) {
            document.getElementById('downloadf').src = "";
            document.getElementById('downloadf').src = "../../../PostFileBytes.aspx";
            },function(err){alert(err._message);});
        }
    }
    
    // 学期末核算方法
    function GetSumData() {
        var tid = parseInt(request('TeacherId'));
        PageMethods.GetAStupidDemand(tid, function(srcc){
                //debugger;
                var list = [];
                var trList = $("#tblHead tr");
                var stuJsonList = "";
                var jsonList = "";
                var scoreInputID = 0;
                var gongkaiID = 0;
                var xuancuanID = 0;
                var jiluID = 0;
                for (var i=1; i<trList.length; i++) {
                    var monthList = $(trList[i]).find("td");
                    for(var v = 0; v < monthList.length; v++) {
                        if(monthList[v].innerText.indexOf("学分录入") >= 0) {
                            scoreInputID = parseInt(monthList[v].getAttribute("itemID"));
                            continue;
                        }
                        if(monthList[v].innerText.indexOf("公开") >= 0) {
                            gongkaiID = parseInt(monthList[v].getAttribute("itemID"));
                            continue;
                        }
                        if(monthList[v].innerText.indexOf("宣传") >= 0) {
                            xuancuanID = parseInt(monthList[v].getAttribute("itemID"));
                            continue;
                        }
                        if(monthList[v].innerText.indexOf("记录") >= 0) {
                            jiluID = parseInt(monthList[v].getAttribute("itemID"));
                            continue;
                        }
                    }
                    
                    if ($(trList[i]).find("table").length == 0) {
                        continue;
                    }
                    
                    var monthScore = 0;
                    for(var m = 0; m < monthList.length; m++) {
                        var montht = monthList[m].getAttribute("DataMonth") == null ? 0 : parseInt(monthList[m].getAttribute("DataMonth"));
                        if(montht == 0) continue;
                        for(var a = 0; a < srcc.length; a ++) {
                            if(srcc[a].StartTime.getMonth()+1 == montht)
                            {
                                monthList[m].innerText = 6;
                                monthScore += 6;
                                break;
                            }
                        }
                        
                    }
                    
                    var inputList = $(trList[i]).find("input");
                    var spanRemark = $(trList[i]).find("span")[0];
                    var inputState = false;
                    var stuList = [];
                    var teacherID;
                    for (var j=0; j<inputList.length; j++) {
                        var str = inputList[j].id;
                        str = str.replace("txt_", "");
                        var arr = str.split("_");
                        var moralityItemID = parseInt(arr[0]);
                        teacherID = parseInt(arr[1]);
                        var param = parseFloat(val);
                        var score = inputList[j].getAttribute("Score") == null ? -1 : parseFloat(inputList[j].getAttribute("Score"));
                        var operator = inputList[j].getAttribute("Math") == "" ? 0 : parseFloat(inputList[j].getAttribute("Math"));
                        
                        if(moralityItemID == scoreInputID) {
                            inputList[j].value = monthScore;
                            continue;
                        }
                        //debugger;
                        for(var x = 0; x < srcc.length; x++) {
                            if(gongkaiID == moralityItemID && srcc[x].Caption.indexOf("公开") >= 0) {
                                if(srcc[x].Score >=0.8)
                                    inputList[j].value = 10;
                                else
                                    inputList[j].value = 0;
                                break;
                            }
                            if(xuancuanID == moralityItemID && srcc[x].Caption.indexOf("宣传") >= 0) {
                                if(srcc[x].Score >=0.8)
                                    inputList[j].value = 10;
                                else
                                    inputList[j].value = 0;
                                break;
                            }
                            if(jiluID == moralityItemID && srcc[x].Caption.indexOf("记录") >= 0) {
                                if(srcc[x].Score >=0.8)
                                    inputList[j].value = 25;
                                else
                                    inputList[j].value = 0;
                                break;
                            }
                            if(srcc[x].Rid == moralityItemID && srcc[x].Score >=0.8) {
                                inputList[j].value = score;
                                break;
                            }
                        }
                        var val = inputList[j].value;
                        if (val != "" && isNaN(val)) {
                            showNoticMsg(str, "请填写数字");
                            return false;
                        }
                        if (val == "") continue;
                        inputState = true;
                      
                        var type = parseInt(inputList[j].getAttribute("MoralityType"));
                        if (type == 2 && param < 0) {
                            stuList = [];
                            showNoticMsg(inputList[j].id, "计次题，请填写大于或等于0的次数");
                            return;
                        }
                        if (isNaN(operator)) operator = "0";
                        var item = "{\"MoralityItemID\": "+moralityItemID+", \"TeacherID\": "+teacherID+", \"Param\": "+param+", \"Score\": "+score+", \"Type\": "+type+", \"Operator\": "+operator+"}";
                        stuList.push(item);
                    } 
                    
                    if (inputState) {
                        stuJsonList = stuList.join(",");
                        stuJsonList = "{\"TeacherID\": "+teacherID+",\"TeacherItems\":[" + stuJsonList + "]";
                        if (spanRemark != undefined && spanRemark.title != "") stuJsonList += ", \"Remark\": \"" + spanRemark.title + "\"";
                        stuJsonList += "}";
                        list.push(stuJsonList);
                    }
                    
                    else {
                        if (spanRemark != undefined && spanRemark.title != "") {
                            studentID = parseInt(spanRemark.id.replace("span_",""));
                            stuJsonList = "{\"TeacherID\": "+teacherID+",\"Remark\": \"" + spanRemark.title + "\"}";
                            list.push(stuJsonList);
                        }
                    }
                }
            }, function(err){} );
    }
        
    function MaxLength(obj)
    {
        var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
        if (obj.getAttribute && obj.value.length>mlength)
        obj.value=obj.value.substring(0,mlength)
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
        </asp:ScriptManager>
        <div class="mainWarp" style="width: 98%; margin: auto;padding-left:5px;">
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <div style="margin-top: 5px;">
                        <h2>
                        <%= ReportTitle %>
                        </h2>
                        <br />
                        <input type="button" class="commonBut" id="btnReset" value="重置" onclick="reflesh();" />&nbsp;
                        <input type="button" class="commonBut" id="btnSave" value="保存" onclick="saveList();" />
                        <input type="button" class="commonBut" id="btnSum" value="学期末核算" onclick="GetSumData();"/>
                        <input type="button" class="commonBut" id="btnExport" value="导出Excel" onclick="getExcel()" style="display: none;" />
                        
                    </div>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="btnReflesh" EventName="Click" />
                </Triggers>
            </asp:UpdatePanel>
            <br />
             <div id="divTable" style="width:600px;  height:300px; margin:left; overflow: auto;position: relative;">
                <asp:Table ID="tblHead" runat="server" CssClass="useList" CellPadding="0" CellSpacing="0"
                    Style="border-spacing: 0px;margin-top:0px">
                </asp:Table>
            </div>
            <div id="divInputRemark" class="layoutWarp" style="display: none; width: 380px; height: 270px;">
                <div class="title">
                    <span id="spanInputRemarkTitle"></span><a alt="关闭" href="javascript:hideRemark();">关闭</a>
                </div>
                <textarea id="txtRemark" style="width: 370px; height: 200px;"   
                            onkeypress="return (/^[a-zA-Z0-9\u4e00-\u9fa5]+$/g.test(String.fromCharCode(event.keyCode)))"
                           onblur="value=value.replace(/[^\u4e00-\u9fa5a-zA-Z0-9]/g ,'')" 
                           onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" maxlength="50" onkeyup="return MaxLength(this)"></textarea>
                <input type="hidden" id="hidTeacherID" />
                <div style="text-align: center;">
                    <input type="button" class="commonBut" value="保存" onclick="saveRemark();" />
                    <input type="button" class="commonBut" value="取消" onclick="hideRemark();" /></div>
            </div>
        </div>
        <asp:Button ID="btnReflesh" runat="server" Style="display: none;" OnClick="btnReflesh_Click" />
        <div id="upInputPop" class="layoutWarp" style="display: none">
            <iframe src="" name="downloadf" id="downloadf" style="height: 80%;width: 95%;overflow-y:auto;overflow-x:none;" frameborder="0"   ></iframe>
        </div>
    </form>
</body>
</html>
<script language="javascript">
        var h=0;
        var w=0;
        if(document.body.scrollHeight>document.getElementById("tblHead").clientHeight)
        {
            h = document.getElementById("tblHead").clientHeight;
        }
        else
        {
            h = document.body.scrollHeight-100;
        }
        if(document.body.scrollWidth>document.getElementById("tblHead").clientWidth)
        {
            w = document.getElementById("tblHead").clientWidth+20;
        }
        else
        {
            w = document.body.scrollWidth-30;
        }
        
        if(h>0 && w>0)
        {
            document.getElementById("divTable").style.height=h+"px";
            document.getElementById("divTable").style.width=w+"px";
        }
</script>
<script>
historyId=<%=historyId %>
</script>